package com.smart.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ReflectTest {

	@SuppressWarnings("unchecked")
	public static Car initByDefaultConst() throws Throwable {

		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		Class clazz = classLoader.loadClass("com.smart.reflect.Car");
		Constructor cons = clazz.getDeclaredConstructor((Class[]) null);

		Car car = (Car) cons.newInstance();

		Method setBrand = clazz.getMethod("setBrand", String.class);
		setBrand.invoke(car, "红旗A123");
		Method setCollor = clazz.getMethod("setColor", String.class);
		setCollor.invoke(car, "红色");
		Method setMaxSpeed = clazz.getMethod("setMaxSpeed", int.class);
		setMaxSpeed.invoke(car, 300);
		return car;
	}
	
	public static void main(String[] args) throws Throwable {
		Car car = initByDefaultConst();
		car.introduce();
	}
}
